package cc.wx.fom.ds;

/**
 * 创建于:2015年7月29日<br>
 * 圆弧实现
 * 
 * @author WangXue
 * @version 1.0.0
 */
public class Edge implements IEdge {
	private IVertex headVertex;
	private IVertex tailVertex;

	public Edge(IVertex headVertex, IVertex tailVertex) {
		super();
		this.headVertex = headVertex;
		this.tailVertex = tailVertex;
	}

	@Override
	public String toString() {
		return "E(h:" + headVertex.getId() + ",t:" + tailVertex.getId() + ")";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof IEdge) {
			// 无向图，头=尾或者尾=头，均认为是同一条边
			IEdge edge = (IEdge) obj;
			IVertex headVertex2 = edge.getHeadVertex();
			IVertex tailVertex2 = edge.getTailVertex();
			if (headVertex.equals(headVertex2) && tailVertex.equals(tailVertex2)) {
				return true;
			}
			if (headVertex.equals(tailVertex2) && tailVertex.equals(headVertex2)) {
				return true;
			}
			return false;
		} else {
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#setHeadVertex(cc.wx.fom.ds.IVertex)
	 */
	@Override
	public void setHeadVertex(IVertex vertex) {
		this.headVertex = vertex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#setTailVertex(cc.wx.fom.ds.IVertex)
	 */
	@Override
	public void setTailVertex(IVertex vertex) {
		this.tailVertex = vertex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#getHeadVertex()
	 */
	@Override
	public IVertex getHeadVertex() {
		return headVertex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#getTailVertex()
	 */
	@Override
	public IVertex getTailVertex() {
		return tailVertex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#startWith(cc.wx.fom.ds.IVertex)
	 */
	@Override
	public boolean startWith(IVertex v) {
		if (headVertex.equals(v)) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#endWith(cc.wx.fom.ds.IVertex)
	 */
	@Override
	public boolean endWith(IVertex v) {
		if (tailVertex.equals(v)) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IEdge#areAdjacent(cc.wx.fom.ds.IVertex,
	 * cc.wx.fom.ds.IVertex)
	 */
	@Override
	public boolean areAdjacent(IVertex u, IVertex v) {
		if (headVertex.equals(u) && tailVertex.equals(v)) {
			return true;
		}
		return false;
	}

}
